package com.bw.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.bw.domain.SysUser;
import com.bw.service.SysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class UserDetailServieImpl implements UserDetailsService {
    @Autowired
    SysUserService sysUserService;
    @Resource
    RedisTemplate redisTemplate;
    @Override
    public UserDetails loadUserByUsername(String s) throws UsernameNotFoundException {
        LambdaQueryWrapper<SysUser> queryWrapper = new LambdaQueryWrapper<>();
        LambdaQueryWrapper<SysUser> eq = queryWrapper.eq(SysUser::getUsername, s);
        SysUser one = sysUserService.getOne(queryWrapper);
        //获取权限
        List<String> list = sysUserService.getauthbyid(one.getId());
        List<SimpleGrantedAuthority> collect = list.stream().map(SimpleGrantedAuthority::new).collect(Collectors.toList());
        //存redis
        redisTemplate.opsForValue().set(one.getId()+"",collect);

        return new User(s,one.getPwd(),collect);
    }
}
